/* Gauge - Performance Measurement Framework for Mobile Platforms
 * Copyright (C) 2013, Juha Hytonen <juha.a.hytonen@gmail.com>
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

package org.gauge.ExampleBenchmarks.Workloads;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.IBinder;
import android.os.RemoteException;

import org.gauge.Workload;
import org.joda.time.DateTime;

/**
 * Simple workload that simulates browsing to 10 different sites with
 * five second intervals.
 */
public final class WebBrowsing implements Workload, ServiceConnection {
	
	private static final int MillisecsToWaitBetweenSites = 5000;
	
	private Context context;
	private WBSInterface service;
	private List<String> sites;
	private volatile boolean keepRunning;
	private Lock lock;
	private Condition serviceAvailable;
	
	public WebBrowsing() {
		keepRunning = true;
		lock = new ReentrantLock();
		serviceAvailable = lock.newCondition();
		initialiseSites();
	}
	
	private void initialiseSites() {
		sites = new ArrayList<String>();
		sites.add("http://www.google.com");
		sites.add("http://www.yahoo.com");
		sites.add("http://www.amazon.com");
		sites.add("http://www.twitter.com");
		sites.add("http://www.msn.com");
		sites.add("http://www.microsoft.com");
		sites.add("http://www.tumblr.com");
		sites.add("http://www.ebay.com");
		sites.add("http://www.conduit.com");
		sites.add("http://www.ask.com");
	}

	@Override
	public void SetContext(Context context) {
		this.context = context;
	}

	@Override
	public void Stop() {
		keepRunning = false;
	}
	
	@Override
	public void Run() {
		startActivity();
		startService();
		waitUntilServiceConnected();
		browseSites();
		exit();
		stopService();
	}
	
	private void startActivity() {
		Intent activityIntent = new Intent(context, WebBrowsingActivity.class);
		activityIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
		context.startActivity(activityIntent);
	}
	
	private void startService() {
		Intent serviceIntent = new Intent(context, WebBrowsingService.class);
		context.bindService(serviceIntent, this, Context.BIND_AUTO_CREATE);
		// -> onServiceConnected()
	}
	
	private void waitUntilServiceConnected() {
		lock.lock();
		while(true) {
			try {
				serviceAvailable.await();
				break;
			}
			catch(InterruptedException e) {
			}
		}
		lock.unlock();
	}

	@Override
	public void onServiceConnected(ComponentName className, IBinder binder) {
		service = WBSInterface.Stub.asInterface(binder);
		lock.lock();
		serviceAvailable.signal();
		lock.unlock();
	}
	
	private void browseSites() {
		int currentSite = 0;
		while(keepRunning && (currentSite < sites.size())) {
			goToSite(sites.get(currentSite));
			waitForSiteToLoad();
			currentSite++;
		}
	}
	
	private void goToSite(String site) {
		try {
			service.GoTo(site);
		}
		catch(RemoteException e) {
		}
	}
	
	private void waitForSiteToLoad() {
		org.gauge.Util.WaitFor(MillisecsToWaitBetweenSites);
	}
	
	private void exit() {
		try {
			service.Exit();
		}
		catch(RemoteException e) {
		}
	}
	
	private void stopService() {
		context.unbindService(this);
		// -> onServiceDisconnected()
	}

	@Override
	public void onServiceDisconnected(ComponentName className) {
		service = null;
	}

}
